/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package control;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import model.Processo;

/**
 *
 * @author
 * Abraão de Santana
 * Jorge Thiago
 * Leandro C. Arantes
 */
public class CriadorDeProcessos {

    private static Vector<Processo> processCreated = new Vector<Processo>();

    public static void loadProcess()
    {
        processCreated.clear();
        JFileChooser chooser = new JFileChooser();

        if ( chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION )
        {
            File file = chooser.getSelectedFile();

            try
            {
                BufferedReader in = new BufferedReader(new FileReader(file));
                String line = in.readLine();
                int i = 0;

                while ( line != null )
                {
                    i++;
                    String[] processData = line.split(", ");

                    Processo process = new Processo();
                    process.setId(i);
                    process.setTempoDeChegada( Integer.parseInt(processData[0]) );
                    process.setPrioridade( Integer.parseInt(processData[1]) );
                    process.setTempoDoProcessador( Integer.parseInt(processData[2]) );
                    process.setmBytes( Integer.parseInt(processData[3]) );
                    process.setImpressoras( Integer.parseInt(processData[4]) );
                    process.setScanner( Integer.parseInt(processData[5]) );
                    process.setModem( Integer.parseInt(processData[6]) );
                    process.setDrivreCD( Integer.parseInt(processData[7]) );

                    processCreated.add(process);

                    line = in.readLine();
                }
            }

            catch (FileNotFoundException ex)
            {
                Logger.getLogger(CriadorDeProcessos.class.getName()).log(Level.SEVERE, null, ex);
            }            catch (IOException ex)
            {
                Logger.getLogger(CriadorDeProcessos.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void verificaProcessosNovos( int time )
    {
        for ( Processo process : processCreated )
        {
            if ( process.getTempoDeChegada() == time )
            {
                SistemaOperacional.submitProcess(process);
            }
        }
    }

}
